package com.liang.controller;


import com.liang.common.lang.Result;
import com.liang.entity.CKaoshi;
import com.liang.service.CKaoshiService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李昂
 * @since 2020-11-30
 */
@RestController
@RequestMapping("/kaoshi")
@AllArgsConstructor
public class CKaoshiController {


    private CKaoshiService kaoshiService;

    /**
     * @Author 李昂
     * @Description 插入考试主题 以及考试人员成绩
     * @Date 2020/12/4 14:42
     */
    @PostMapping
    public Result addKaoshiRecord(@RequestParam("title") String title, @RequestBody(required = false) List<CKaoshi> kaoshis) {

        //通过循环遍历 给title和clazz赋值
        kaoshis.forEach(item -> {
            item.setTitle(title);
            item.setClazz(title.substring(0, 2));
        });

        if (kaoshiService.insertScores(kaoshis)) {
            return Result.succ();
        } else {
            return Result.fail();
        }

    }

    /**
     * @Author 李昂
     * @Description 根据title子段进行分组 显示考试类型,当title有值的时候 进行匹配查询
     * @Date 2020/12/4 15:45
     */
    @GetMapping
    public Result showKaoshiType(@RequestParam(value = "title", required = false) String title) {
        List<CKaoshi> kaoshis = kaoshiService.showAllKaoshi(title);
        return Result.succ(kaoshis);
    }

    /**
     * @Author 李昂
     * @Description 显示本次考试成员的成绩
     * @Date 2020/12/4 17:29
     */
    @GetMapping("/score")
    public Result showScore(@RequestParam("title") String title) {
        List<CKaoshi> kaoshis = kaoshiService.showScore(title);
        return Result.succ(kaoshis);
    }

    /**
     * @Author 李昂
     * @Description 删除某一次考试的所有成绩
     * @Date 2020/12/4 17:49
     */
    @DeleteMapping
    public Result deleteKaoshi(@RequestParam("title") String title) {
        if (kaoshiService.deleteKaoshi(title)) {
            return Result.succ();
        } else {
            return Result.fail();
        }
    }


    //查询个人考试成绩
    @GetMapping("/sno")
    public Result getGradeBySno(@RequestParam("sno") String sno) {
        return Result.succ(kaoshiService.getGradeBySno(sno));
    }


}
